package com.task.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.entity.Task;
import com.setting.entity.JsonOrg;
import com.setting.entity.JsonOrgMenu;
import com.setting.service.OrgService;
import com.setting.service.SysdicService;
import com.task.entity.JsonTask;
import com.task.service.TaskService;
import com.utils.JsonResult;
import com.utils.JsonSysdic;

/**
 * @Title: TaskController.java
 * @Package com.task.controller
 * @author 孙梦楠
 * @date 2017年9月4日 下午4:45:09
 * @version V1.0.0
 */

@RestController
@RequestMapping(value = "/task")
public class TaskController {

	@Autowired
	private TaskService taskservice;
	
	@Autowired
	private OrgService orgservice;
	
	@Autowired
	private SysdicService sysdicservice;

	// 条件查询
	@RequestMapping(value = "", method = RequestMethod.POST)
	public JsonResult<List<JsonTask>> listtask(@RequestBody JsonTask jsontask,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String taskname = jsontask.getTaskname();
		String taskstatus = jsontask.getTaskstatus();

		return taskservice.listtask(taskname, taskstatus, opeorgcookie, opetypecookie, languagecookie);
	}
	
	//展示所有已启动的任务
	@RequestMapping(value = "/listtask_started", method = RequestMethod.POST)
	public JsonResult<List<JsonTask>> listtask_started(@RequestBody JsonTask jsontask,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String taskname = jsontask.getTaskname();
		String taskstatus = "1";// 0 未启动 ，1 启动

		return taskservice.listtask(taskname, taskstatus, opeorgcookie, opetypecookie, languagecookie);
	}
	
	//展示所有未启动的任务
	@RequestMapping(value = "/listtask_notstarted", method = RequestMethod.POST)
	public JsonResult<List<JsonTask>> listtask_notstarted(@RequestBody JsonTask jsontask,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String taskname = jsontask.getTaskname();
		String taskstatus = "0";// 0 未启动 ，1 启动

		return taskservice.listtask(taskname, taskstatus, opeorgcookie, opetypecookie, languagecookie);
	}
	
	//手动执行任务
	@RequestMapping(value = "/start_task", method = RequestMethod.POST)
	public JsonResult<Task> startTask(@RequestBody JsonTask jsontask){
		
		Integer taskid =jsontask.getTaskid();
		return taskservice.startTask(taskid);
	}
	
	
	//小区列表
	@RequestMapping(value = "/list_area", method = RequestMethod.POST)
	public JsonResult<List<JsonOrgMenu>> list_area(@RequestBody JsonOrg jsonorg,
			@CookieValue(value = "languagecookie", required = false) String languagecookie,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie){
		String orgfather = opeorgcookie;
		
		return orgservice.listarea(orgfather,languagecookie);
		
	}
	
	//字典 列出job类型
	@RequestMapping(value = "/listsysdicJobtype", method = RequestMethod.GET)
	public JsonResult<List<JsonSysdic>> listsysdicTerminaltype(
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String sysdictype = sysdicservice.gettype(opetypecookie, "5");// job  5
		Integer state = 612;
		String Message = "job字段字典列表";

		return sysdicservice.listsysdic(sysdictype, state, Message, languagecookie);
	}
	
	
//	// 任务状态菜单
//	@RequestMapping(value = "/list_task_status", method = RequestMethod.GET)
//	public JsonResult<List<JsonSysdic>> listtaskstatus(
//			@CookieValue(value = "languagecookie", required = false) String languagecookie) {
//
//		return taskservice.listtaskstatus(languagecookie);
//	}

	// 增加
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public JsonResult<JsonTask> addtask(@RequestBody JsonTask jsontask,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {
		
		String taskname = jsontask.getTaskname();
		String taskbegin = jsontask.getTaskbegin();
		String taskend = jsontask.getTaskend();
		Integer taskperiod = jsontask.getTaskperiod();
		String monday = jsontask.getMonday();
		String tuesday = jsontask.getTuesday();
		String wednesday = jsontask.getWednesday();
		String thursday = jsontask.getThursday();
		String friday = jsontask.getFriday(); 
		String saturday = jsontask.getSaturday();
		String sunday = jsontask.getSunday();
		Integer taskadditionalnum = jsontask.getTaskadditionalnum();
		Integer taskadditionaltime = jsontask.getTaskadditionaltime();
		Integer tasklevel = jsontask.getTasklevel();
		String tasktime = jsontask.getTasktime();
		String jobtype = jsontask.getJobtype();

		return taskservice.addtask(taskname, taskbegin, taskend, taskperiod, monday, tuesday, wednesday, thursday,
				friday, saturday, sunday, taskadditionalnum, taskadditionaltime, tasklevel, opeorgcookie,
				opetypecookie,languagecookie,tasktime,jobtype);
	}
	
	//删除
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public JsonResult<Task> deleteTask(@RequestBody JsonTask jsontask){
		Integer  taskid =jsontask.getTaskid();
		return taskservice.deleteTask(taskid);
	}
	//修改界面
	@RequestMapping(value = "/getbyid", method = RequestMethod.POST)
	public JsonResult<JsonTask> updateview(@RequestBody JsonTask jsontask,
			@CookieValue(value = "languagecookie", required = false) String languagecookie){
		Integer  taskid =jsontask.getTaskid();
		
		return taskservice.updateview(taskid, languagecookie);
	}
	
	//修改
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonResult<JsonTask> updatetask(@RequestBody JsonTask jsontask,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {
		
		Integer taskid = jsontask.getTaskid();
		String taskname = jsontask.getTaskname();
		String taskbegin = jsontask.getTaskbegin();
		String taskend = jsontask.getTaskend();
		Integer taskperiod = jsontask.getTaskperiod();
		String Monday = jsontask.getMonday();
		String Tuesday = jsontask.getTuesday();
		String Wednesday = jsontask.getWednesday();
		String Thursday = jsontask.getThursday();
		String Friday = jsontask.getFriday(); 
		String Saturday = jsontask.getSaturday();
		String Sunday = jsontask.getSunday();
		Integer taskadditionalnum = jsontask.getTaskadditionalnum();
		Integer taskadditionaltime = jsontask.getTaskadditionaltime();
		Integer tasklevel = jsontask.getTasklevel();
		String tasktime = jsontask.getTasktime();
		String jobtype = jsontask.getJobtype();

		return taskservice.updatetask(taskid,taskname, taskbegin, taskend, taskperiod, Monday, Tuesday, Wednesday, Thursday,
				Friday, Saturday, Sunday, taskadditionalnum, taskadditionaltime, tasklevel, opeorgcookie,
				opetypecookie,languagecookie,tasktime,jobtype);
	}
}
